package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.system.domain.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TOrderDetail;
import com.ruoyi.system.mapper.TOrderMapper;
import com.ruoyi.system.domain.TOrder;
import com.ruoyi.system.service.ITOrderService;
import com.ruoyi.common.core.text.Convert;

/**
 * 订单管理Service业务层处理
 *
 * @author ruoyi
 * @date 2020-10-16
 */
@Service
public class TOrderServiceImpl implements ITOrderService {

    private static final Logger log = LoggerFactory.getLogger(TOrderServiceImpl.class);


    @Autowired
    private TOrderMapper tOrderMapper;

    /**
     * 查询订单管理
     *
     * @param orderId 订单管理ID
     * @return 订单管理
     */
    @Override
    public TOrder selectTOrderById(Long orderId) {
        return tOrderMapper.selectTOrderById(orderId);
    }

    /**
     * 查询订单管理列表
     *
     * @param tOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<TOrder> selectTOrderList(TOrder tOrder) {
        return tOrderMapper.selectTOrderList(tOrder);
    }

    /**
     * 新增订单管理
     *
     * @param tOrder 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTOrder(TOrder tOrder) {
        int rows = tOrderMapper.insertTOrder(tOrder);
        insertTOrderDetail(tOrder);
        return rows;
    }

    /**
     * 修改订单管理
     *
     * @param tOrder 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTOrder(TOrder tOrder) {
        tOrderMapper.deleteTOrderDetailByOrderId(tOrder.getOrderId());
        insertTOrderDetail(tOrder);
        return tOrderMapper.updateTOrder(tOrder);
    }

    /**
     * 删除订单管理对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTOrderByIds(String ids) {
        tOrderMapper.deleteTOrderDetailByOrderIds(Convert.toStrArray(ids));
        return tOrderMapper.deleteTOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除订单管理信息
     *
     * @param orderId 订单管理ID
     * @return 结果
     */
    @Override
    public int deleteTOrderById(Long orderId) {
        tOrderMapper.deleteTOrderDetailByOrderId(orderId);
        return tOrderMapper.deleteTOrderById(orderId);
    }

    @Override
    public String importOrder(List<TOrder> orderList, boolean updateSupport) {
        if (StringUtils.isNull(orderList) || orderList.size() == 0) {
            throw new BusinessException("导入订单数据不能为空！");
        }

        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (TOrder tOrder : orderList
                ) {
            try {
                TOrder o = tOrderMapper.selectTOrderByOrderNo(tOrder.getOrderNo());

                if (o == null) {
                    this.insertTOrder(tOrder);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、订单号 " + tOrder.getOrderNo() + " 导入成功");
                } else if (updateSupport) {
                    tOrder.setOrderId(o.getOrderId());
                    this.updateTOrder(tOrder);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、订单号 " + tOrder.getOrderNo() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、订单号 " + tOrder.getOrderNo() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、订单号 " + tOrder.getOrderNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }

        return successMsg.toString();
    }

    @Override
    public Double selectTOrderListTotalSum(TOrder tOrder) {

        Double   f   =  tOrderMapper.selectTOrderListTotalSum(tOrder);
        BigDecimal b   =   new   BigDecimal(f);
        Double   f1   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

    @Override
    public List<TOrder> selectTOrderByIds(List ids) {
        return tOrderMapper.selectTOrderByIds(ids);
    }

    /**
     * 新增订单明细信息
     *
     * @param tOrder 订单管理对象
     */
    public void insertTOrderDetail(TOrder tOrder) {
        List<TOrderDetail> tOrderDetailList = tOrder.getTOrderDetailList();
        Long orderId = tOrder.getOrderId();
        if (StringUtils.isNotNull(tOrderDetailList)) {
            List<TOrderDetail> list = new ArrayList<TOrderDetail>();
            for (TOrderDetail tOrderDetail : tOrderDetailList) {
                tOrderDetail.setOrderId(orderId);
                list.add(tOrderDetail);
            }
            if (list.size() > 0) {
                tOrderMapper.batchTOrderDetail(list);
            }
        }
    }
}
